package com.fdq.dianping.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fdq.dianping.entity.Result;
import com.fdq.dianping.entity.SeckillVoucher;
import com.fdq.dianping.entity.Voucher;
import com.fdq.dianping.service.ISeckillVoucherService;
import com.fdq.dianping.service.IVoucherService;
import com.fdq.dianping.util.RedisClient;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.fdq.dianping.util.Constants.SeckillVoucher_Stock;
import static com.fdq.dianping.util.Constants.SeckillVoucher_Stock_TTL;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fdq
 * @since 2024-12-29
 */
@RestController
@RequestMapping("/voucher")
public class VoucherController {
    @Resource
    private IVoucherService voucherService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    //redis自己写的类
    private  RedisClient redisClient;

    @Resource
    private ISeckillVoucherService seckillVoucherService;

    /*
    * 新增秒杀券
    * */
    @PostMapping("/seckill")
    public Result addSeckillVoucher(@RequestBody Voucher voucher){
        return voucherService.addSeckillVoucher(voucher);
    }

    /*
     * 新增普通优惠券
     * */
    @PostMapping("/normal")
    public Result addNormalVoucher(@RequestBody Voucher voucher){
        return voucherService.addNormalVoucher(voucher);
    }

    /*
    * 删除普通优惠券
    * */
    @DeleteMapping("/normal")
    public Result deleteNormalVoucher(Long id){
        return voucherService.deleteNormalVoucher(id);
    }

    /*
    * 删除秒杀优惠券
    * */
    @DeleteMapping("/seckill")
    public Result deleteSeckillVoucher(Long id){
        return voucherService.deleteSeckillVoucher(id);
    }

    /*
    * 更新普通优惠券
    * */
    @PutMapping("/normal")
    public Result updateNormalVoucher(@RequestBody Voucher voucher){
        boolean voucherUpdate = voucherService.updateById(voucher);
        if(voucherUpdate){
            return Result.success();
        }else {
            return Result.error("修改信息失败");
        }
    }

    /*
    * 更新秒杀优惠券
    * */
    @PutMapping("/seckill")
    public Result updateSeckillVoucher(@RequestBody Voucher voucher){
        return voucherService.updateSeckillById(voucher);
    }

    /*
    * 根据商户id查询优惠券
    * */
    @GetMapping("/getVoucher")
    public Result getVoucherByShopId(Long shopId){
        QueryWrapper<Voucher> voucherQueryWrapper = new QueryWrapper<>();
        voucherQueryWrapper.eq("shop_id", shopId);
        List<Voucher> vouchers = voucherService.list(voucherQueryWrapper);
        //如果是秒杀券添加生效时间、失效时间、库存属性
        vouchers.forEach(voucher -> {
            if(voucher.getType() == 1){
                SeckillVoucher seckillVoucher = seckillVoucherService.getById(voucher.getId());
                voucher.setBeginTime(seckillVoucher.getBeginTime());
                voucher.setEndTime(seckillVoucher.getEndTime());
                voucher.setStock(seckillVoucher.getStock());
                //把将要开始秒杀的优惠券库存信息写入redis中用于之后的秒杀处理
                //因为如果每次查询优惠券时写入库存信息可能导致超卖
                if (seckillVoucher.getBeginTime().isAfter(LocalDateTime.now())){
                    stringRedisTemplate.opsForValue().set(SeckillVoucher_Stock+voucher.getId(), String.valueOf(voucher.getStock()));
                    stringRedisTemplate.expire(SeckillVoucher_Stock+voucher.getId(), SeckillVoucher_Stock_TTL, TimeUnit.MINUTES);
                }

            }

        });
        return Result.success(vouchers);
    }

    /*
    * 根据优惠券id查询优惠券信息
    * */
    @GetMapping("/getById")
    public Result getById(Long id){
        QueryWrapper<Voucher> voucherQueryWrapper = new QueryWrapper<>();
        voucherQueryWrapper.eq("id", id);
        Voucher voucher = voucherService.getOne(voucherQueryWrapper);
        return Result.success(voucher);
    }
}
